﻿using System;
using System.Diagnostics;
using EventAggregator;

namespace EAPerfTest
{
    internal class Program
    {
        private static EventHub _eventHub;

        public Program(int n)
        {
            N = n;

            for (int i = 0; i < N; i++)
            {
                this.Subscribe(_eventHub, i, HandleEvent);
            }
        }

        public int N { get; private set; }

        private static void Main(string[] args)
        {
            for (int i = 1000; i < 1000000; i += 1000)
            {
                Stopwatch watch = Stopwatch.StartNew();
                RunAllTests(i);
                watch.Stop();

                Console.WriteLine("Total count of events: {0}\nTotal time: {1} ms\nSingle event (send+receive): {2} ms",
                                  i, watch.ElapsedMilliseconds, (double) watch.ElapsedMilliseconds/i);
                Console.WriteLine("---------------------------------------------------");
            }

            Console.ReadLine();
        }

        private static void RunAllTests(int n)
        {
            _eventHub = new EventHub();

            var receiver = new Program(n);
            var sender = new Program(n);

            sender.RunTest(n);
        }

        private void HandleEvent(EventMessage eventMessage)
        {
        }

        public void RunTest(int n)
        {
            for (int i = 0; i < n; i++)
            {
                this.Send(_eventHub, i);
            }
        }
    }
}